<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Logic\LiveLogic;
use App\Middleware\AuthMiddleware;
use App\Utils\Enum\UserEnum;
use Exception;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Psr\Http\Message\ResponseInterface;

#[AutoController]
class AnchorController extends AbstractController
{
    /**
     * 主播列表.
     */
    public function list(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        $input = $this->request->all();
        $res = LiveLogic::getAnchorList($userid, $input);
        return $this->apiReturn(200, 'succ', $res);
    }

    /**
     * 获取正在直播的直播
     * @return ResponseInterface
     */
    public function liveAnchor(): ResponseInterface
    {
        $input = $this->request->all();
        $res = LiveLogic::getLiveAnchorList($input);
        return $this->apiReturn(200, 'succ', $res);
    }

    /**
     * 关注/取关主播.
     */
    #[Middleware(AuthMiddleware::class)]
    public function follow(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $anchorUserId = (int)$this->request->input('anchorUserId', 0);
        if (!$anchorUserId) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $followed = LiveLogic::followAnchor($userid, $anchorUserId);
            return $this->apiReturn(200, 'succ', ['followed' => $followed]);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取主播详情.
     */
    public function detail(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        $roomId = $this->request->input('roomId', 0);
        if (!$roomId) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $info = LiveLogic::getAnchorDetail($userid, $roomId);
            return $this->apiReturn(200, '获取成功', $info);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 排行榜.
     */
    public function rank(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        $topNum = $this->request->input('topNum', 10);
        $res = LiveLogic::getAnchorRankList($userid, $topNum);
        return $this->apiReturn(200, '获取成功', $res);
    }

    /**
     * 主播排行榜
     * @return ResponseInterface
     */
    public function hotAnchor(): ResponseInterface
    {
        $res = LiveLogic::getAnchorRankList(0, 30);
        return $this->apiReturn(200, 'succ', $res);
    }

    /**
     * 获取最新入驻记录
     * @return ResponseInterface
     */
    #[Middleware(AuthMiddleware::class)]
    public function settledResult(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $result = LiveLogic::getLastSettledResult($userid);
        return $this->apiReturn(200, 'succ', $result);
    }


    /**
     * 入驻申请
     * @return ResponseInterface
     */
    #[Middleware(AuthMiddleware::class)]
    public function settled(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $roleId = Context::get('roleId', 0);
        if ($roleId != UserEnum::USER_TYPE_NORMAL) {
            return $this->apiReturn(400, '当前身份不可入驻专家');
        }
        $input = $this->request->all();
        if (empty($input['contact']) || empty($input['telephone']) || empty($input['introduce'])) {
            return $this->apiReturn(400, '请认真完整填写申请表单');
        }
        try {
            $result = LiveLogic::settled($userid, $input);
            return $this->apiReturn(200, '提交成功', $result);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取礼物列表
     * @return ResponseInterface
     */
    public function gift(): ResponseInterface
    {
        return $this->apiReturn(200, 'succ', LiveLogic::getGiftList());
    }
}
